﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Retirement_Admin_changeDetails : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            Dictionary<string, Object> applicationInfo = (Dictionary<string, Object>)Session["R_applicationInfo"];
            if (Util.isValueNotNull(applicationInfo, "firstName"))
            {
                this.tb_firstName.Text = applicationInfo["firstName"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "lastName"))
            {
                this.tb_lastName.Text = applicationInfo["lastName"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "nationality"))
            {
                this.tb_nationality.Text = applicationInfo["nationality"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "nric"))
            {
                this.tb_nric.Text = applicationInfo["nric"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "dob"))
            {
                this.tb_dob.Text = String.Format("{0:dd/MM/yyyy}", applicationInfo["dob"]);
            }


            if (Util.isValueNotNull(applicationInfo, "gender") && applicationInfo["gender"].Equals("Male"))
            {
                rb_male.Checked = true;
                rb_female.Checked = false;
            }
            else
            {
                rb_male.Checked = false;
                rb_female.Checked = true;
            }

            if (Util.isValueNotNull(applicationInfo, "maritalStatus"))
            {
                this.tb_maritalStatus.Text = applicationInfo["maritalStatus"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "smokeStatus") && applicationInfo["smokeStatus"].Equals("Smoker"))
            {
                rb_smoker.Checked = true;
                rb_nonSmoker.Checked = false;
            }
            else
            {
                rb_smoker.Checked = false;
                rb_nonSmoker.Checked = true;     
            }

            //Contact Details
            if (Util.isValueNotNull(applicationInfo, "mailingAddress"))
            {
                this.tb_mailingAddress.Text = applicationInfo["mailingAddress"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "postalCode"))
            {
                this.tb_postalCode.Text = applicationInfo["postalCode"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "mobileNo"))
            {
                this.tb_mobileNo.Text = applicationInfo["mobileNo"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "telNo"))
            {
                this.tb_telNo.Text = applicationInfo["telNo"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "email"))
            {
                this.tb_email.Text = applicationInfo["email"] as string;
            }

            //Occupational Details
            if (Util.isValueNotNull(applicationInfo, "occupation"))
            {
                this.tb_occupation.Text = applicationInfo["occupation"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "jobDuties"))
            {
                this.tb_jobDuties.Text = applicationInfo["jobDuties"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "employer"))
            {
                this.tb_employer.Text = applicationInfo["employer"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "noOfYears"))
            {
                this.tb_noOfYears.Text = Convert.ToString(applicationInfo["noOfYears"]);
            }

            if (Util.isValueNotNull(applicationInfo, "industry"))
            {
                this.tb_industry.Text = applicationInfo["industry"] as string;
            }

            //Income Details
            if (Util.isValueNotNull(applicationInfo, "annualIncomeCurrent"))
            {
                //this.tb_annualEarnedIncome.Text = Convert.ToString(applicationInfo["annualIncomeCurrent"]);
                this.tb_annualEarnedIncome.Text = Convert.ToDecimal(applicationInfo["annualIncomeCurrent"]).ToString("###,###.00");
            }

            if (Util.isValueNotNull(applicationInfo, "annualIncomePrevious"))
            {
                //this.tb_annualEarnedIncomePrevious.Text = Convert.ToString(applicationInfo["annualIncomePrevious"]);
                this.tb_annualEarnedIncomePrevious.Text = Convert.ToDecimal(applicationInfo["annualIncomePrevious"]).ToString("###,###.00");
             }

            if (Util.isValueNotNull(applicationInfo, "anyOtherIncomeSource") && applicationInfo["anyOtherIncomeSource"].Equals("Yes"))
            {
                rb_yesOtherIncome.Checked = true;
                rb_noOtherIncome.Checked = false;
            }
            else
            {
                rb_yesOtherIncome.Checked = false;
                rb_noOtherIncome.Checked = true;
            }

            if (Util.isValueNotNull(applicationInfo, "otherIncomeSource"))
            {
                this.tb_otherIncomeSource.Text = applicationInfo["otherIncomeSource"] as string;
            }
            else
            {
                this.tb_otherIncomeSource.Text = "No Other Income Source";
            }

            if (Util.isValueNotNull(applicationInfo, "otherSourceIncome"))
            {
                //this.tb_otherAnnualEarnedIncome.Text = Convert.ToString(applicationInfo["otherSourceIncome"]);
                this.tb_otherAnnualEarnedIncome.Text = Convert.ToDecimal(applicationInfo["otherSourceIncome"]).ToString("###,###.00");
            }
            else
            {
                this.tb_otherAnnualEarnedIncome.Text = "0.00";

            }

            if (Util.isValueNotNull(applicationInfo, "sourceOfWealth"))
            {
                this.tb_sourceOfWealth.Text = Convert.ToString(applicationInfo["sourceOfWealth"]);
            }

            if (Util.isValueNotNull(applicationInfo, "bankruptcy") && applicationInfo["bankruptcy"].Equals("Yes"))
            {
                rb_yesBankruptcy.Checked = true;
                rb_noBankruptcy.Checked = false;
            }
            else
            {
                rb_yesBankruptcy.Checked = false;
                rb_noBankruptcy.Checked = true;
            }

            //Plan Details
            int age = -1;

            if (Util.isValueNotNull(applicationInfo, "dob"))
            {
                age = Util.getAge((DateTime)applicationInfo["dob"]);
               
                if (age >= 0)
                {
                    this.tb_basicPlanDuration.Text = Convert.ToString(99 - age) + " Years";
                }

                if (Util.isValueNotNull(applicationInfo, "criticalCarePlusRider") && applicationInfo["criticalCarePlusRider"].Equals("Yes"))
                {
                    this.tb_plusRiderDuration.Text = Convert.ToString(65 - age) + " Years";
                }

                if (Util.isValueNotNull(applicationInfo, "criticalCareWaiverRider") && applicationInfo["criticalCareWaiverRider"].Equals("Yes"))
                {
                    this.tb_waiverRiderDuration.Text = Convert.ToString(65 - age) + " Years";
                }
            }

            if (Util.isValueNotNull(applicationInfo, "planName"))
            {
                this.tb_planName.Text = applicationInfo["planName"] as string;
            }

            if (Util.isValueNotNull(applicationInfo, "basicSumInsured"))
            {
                //this.tb_basicSumInsured.Text = Convert.ToString(applicationInfo["basicSumInsured"]);
                this.tb_basicSumInsured.Text = Convert.ToDecimal(applicationInfo["basicSumInsured"]).ToString("###,###.00");
            }

            if (Util.isValueNotNull(applicationInfo, "criticalCarePlusRider") && applicationInfo["criticalCarePlusRider"].Equals("Yes"))
            {
                rb_plusRiderYes.Checked = true;
                rb_plusRiderNo.Checked = false;
            }
            else
            {
                rb_plusRiderYes.Checked = false;
                rb_plusRiderNo.Checked = true;
            }

            if (Util.isValueNotNull(applicationInfo, "plusRiderSumInsured"))
            {
                //this.tb_plusRiderSumInsured.Text = Convert.ToString(applicationInfo["plusRiderSumInsured"]);
                this.tb_plusRiderSumInsured.Text = Convert.ToDecimal(applicationInfo["plusRiderSumInsured"]).ToString("###,###.00");
            }

            if (Util.isValueNotNull(applicationInfo, "criticalCareWaiverRider") && applicationInfo["criticalCareWaiverRider"].Equals("Yes"))
            {
                rb_waiverRiderYes.Checked = true;
                rb_waiverRiderNo.Checked = false;
            }
            else
            {           
                rb_waiverRiderYes.Checked = false;
                rb_waiverRiderNo.Checked = true;
            }

            if (Util.isValueNotNull(applicationInfo, "totalAnnualPremium"))
            {
                //this.tb_totalAnnualPremium.Text = Convert.ToString(applicationInfo["totalAnnualPremium"]);
                this.tb_totalAnnualPremium.Text = Convert.ToDecimal(applicationInfo["totalAnnualPremium"]).ToString("###,###.00");
             }

            //Fund allocation
            if (Util.isValueNotNull(applicationInfo, "choosenFundAllocation"))
            {
                List<Fund> choosenFundAllocation = (List<Fund>)applicationInfo["choosenFundAllocation"];

                this.gv_choosenFundAllocation.DataSource = choosenFundAllocation;
                this.gv_choosenFundAllocation.DataBind();
            }

            //Payment Details
            if (Util.isValueNotNull(applicationInfo, "paymentFrequency") && applicationInfo["paymentFrequency"].Equals("Monthly"))
            {
                rb_monthly.Checked = true;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = false;
            }
            else if (Util.isValueNotNull(applicationInfo, "paymentFrequency") && applicationInfo["paymentFrequency"].Equals("Semi-Annually"))
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = true;
                rb_annually.Checked = false;
            }
            else
            {
                rb_monthly.Checked = false;
                rb_semiAnnually.Checked = false;
                rb_annually.Checked = true;
            }

            if (Util.isValueNotNull(applicationInfo, "paymentMethod") && applicationInfo["paymentMethod"].Equals("Auto"))
            {
                rb_auto.Checked = true;
                rb_direct.Checked = false;
            }
            else
            {
                rb_auto.Checked = false;
                rb_direct.Checked = true;
            }


            if (Util.isValueNotNull(applicationInfo, "paymentAccount"))
            {
                UserAccount userAccount = UserAccount.getUserAccountByNumber((String)applicationInfo["paymentAccount"]);

                if (userAccount == null)
                {
                    this.lb_accountNumber.Text = "Account Not Found";
                }
                else
                {
                    this.lb_accountNumber.Text = userAccount.branchCode + "-" + userAccount.accountNumber + "-" + userAccount.accountTypeCode;
                    this.lb_accountType.Text = userAccount.accountType;
                    this.lb_currency.Text = userAccount.currency;
                }
                

            }   
        }
    }

    //protected void BtnConfirm_Click(object sender, EventArgs e)
    protected void BtnConfirm_Click(object sender, ImageClickEventArgs e)
    {
        PolicyRules policyRules = new PolicyRules();
        Application application = new Application();
        Customer customer = Customer.getCustomerInfo(this.tb_nric.Text);

        Dictionary<string, Object> applicationInfo = (Dictionary<string, Object>)Session["R_applicationInfo"];

        application.filePath = (string)applicationInfo["filePath"];
        application.fundAllocation = (List<Fund>)applicationInfo["choosenFundAllocation"];

        UserAccount userAccount = UserAccount.getUserAccountByNumber((String)applicationInfo["paymentAccount"]);
        if (userAccount==null)
        {
            this.lb_accountNoFoundMsg.Text = "Account Not Found!";
            this.Response.Redirect("./clientDetails.aspx");
        }

        if (userAccount.userId != customer.userId)
        {
            this.lb_accountNoFoundMsg.Text = "Account does not belongs to this client!";
            this.Response.Redirect("./clientDetails.aspx");
        }
        

        //Personal Details
        application.firstName = this.tb_firstName.Text;
        application.lastName =  this.tb_lastName.Text;
        application.nric = this.tb_nric.Text;
        application.nationality = this.tb_nationality.Text;

        if (this.tb_dob.Text != null && !this.tb_dob.Text.Equals(""))
        {
            application.dob = DateTime.ParseExact(this.tb_dob.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }
        else{
            application.dob = null;
        }
       
        if(this.rb_male.Checked == true){

            application.gender = "Male";
        }
        else{
            application.gender = "Female";
        }

        application.maritalStatus = this.tb_maritalStatus.Text;

        if (rb_smoker.Checked == true)
        {
            application.smoker = "Y";
        }
        else
        {
            application.smoker = "N";
        }


        //Contact Details
        application.mailingAddress = this.tb_mailingAddress.Text;
        application.postalCode = this.tb_postalCode.Text;
        application.mobileNo = this.tb_mobileNo.Text;
        application.telNo = this.tb_telNo.Text;
        application.email = this.tb_email.Text;


        //Occupational Details
        application.occupation = this.tb_occupation.Text;
        application.jobDuties = this.tb_jobDuties.Text;
        application.employer = this.tb_employer.Text;
        if (this.tb_noOfYears.Text != null && !this.tb_noOfYears.Text.Equals(""))
        {
            application.noOfYears = Convert.ToDecimal(this.tb_noOfYears.Text);
        }
        else
        {
            application.noOfYears = null;
        }
        application.industry = this.tb_industry.Text;


        //Income Details
        if (this.tb_annualEarnedIncome.Text != null && !this.tb_annualEarnedIncome.Text.Equals(""))
        {
            application.annualIncomeCurrent = Convert.ToDecimal(this.tb_annualEarnedIncome.Text);
        }
        else
        {
            application.annualIncomeCurrent = null;
        }

        if (tb_annualEarnedIncomePrevious.Text != null && !this.tb_annualEarnedIncomePrevious.Text.Equals(""))
        {
            application.annualIncomePrevious = Convert.ToDecimal(this.tb_annualEarnedIncomePrevious.Text);
        }
        else
        {
            application.annualIncomePrevious = null;
        }

        application.otherIncomeSource = this.tb_otherIncomeSource.Text;

        if (this.tb_otherAnnualEarnedIncome.Text != null && !this.tb_otherAnnualEarnedIncome.Text.Equals(""))
        {
            application.otherSourceIncome = Convert.ToDecimal(this.tb_otherAnnualEarnedIncome.Text);
        }
        else
        {
            application.otherSourceIncome = 0m;
        }

        application.sourceOfWealth = this.tb_sourceOfWealth.Text;
        
        if (rb_yesBankruptcy.Checked == true)
        {
           application.bankruptcy = "Y";
        }
        else
        {
            application.bankruptcy = "N";
        }


        //Plan Details
        application.basicSumInsured = Convert.ToDecimal(this.tb_basicSumInsured.Text);

        if (this.tb_plusRiderSumInsured.Text != null && !this.tb_plusRiderSumInsured.Text.Equals(""))
        {
            application.plusRiderSumInsured = Convert.ToDecimal(this.tb_plusRiderSumInsured.Text);
        }
        else
        {
            application.plusRiderSumInsured = null;
        }
        if (this.tb_plusRiderSumInsured.Text != null && !this.tb_plusRiderSumInsured.Text.Equals(""))
        {
            application.waiverRiderPremium = policyRules.getAnnualWaiverRiderPremiumRate(Util.getAge((DateTime)applicationInfo["dob"]), application.gender, application.smoker) * Convert.ToDecimal(this.tb_plusRiderSumInsured.Text) / 1000;
        }
        else
        {
            application.waiverRiderPremium = null;
        }
        application.totalAnnualPremium = Convert.ToDecimal(this.tb_totalAnnualPremium.Text);
        application.benefitType = "The Higher of the Sum Insured Or Account Value";

        if (Session["userID"] != null)
        {
            application.staffId = Convert.ToInt32(Session["userID"]);
        }
        else
        {
            application.staffId = 0;
        }

        if (rb_monthly.Checked == true)
        {
            application.premiumFrequency = "M";
        }
        else if (rb_semiAnnually.Checked == true)
        {
            application.premiumFrequency = "S";
        }
        else
        {
            application.premiumFrequency = "A";
        }

        if (rb_auto.Checked == true)
        {
            application.paymentMethod = "Auto";
        }
        else
        {
            application.paymentMethod = "Direct";
        }


        application.accountId = UserAccount.getUserAccountByNumber((String)applicationInfo["paymentAccount"]).accountID;

        application.fundAllocation = (List<Fund>)applicationInfo["choosenFundAllocation"];
        application.userId = policyRules.getUserId(this.tb_nric.Text);

        string policyNo = application.create();


        this.Response.Redirect("./certificate.aspx?policyNo=" + policyNo);
    }

    //protected void btn_reupload_Click(object sender, EventArgs e)
    protected void btn_reupload_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("./applicationFormUpload.aspx");
    }


}